package com.ruoyi.utils.qny;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.qiniu.http.Client;
import com.qiniu.util.Auth;
import com.qiniu.util.StringMap;
import com.ruoyi.SSL.domain.SslApply;
import com.ruoyi.common.core.domain.AjaxResult;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


public class QnyCdnDomainUtils {


    private final static String QNY_BASE_URI="https://api.qiniu.com";

    //创建授权对象的方法
    public static Auth createAuth(String keyId, String keySecret) {
        return Auth.create(keyId, keySecret);
    }


    public static AjaxResult listCdnDomain(String keyId, String keySecret, String platform){
        try {
            Auth auth = createAuth(keyId, keySecret);
            StringMap headers = auth.authorizationV2(QNY_BASE_URI+"/domain?product=cdn&limit=100", "GET", "".getBytes(), Client.FormMime);
            HttpRequest request = HttpRequest.get(QNY_BASE_URI+"/domain?product=cdn&limit=100");
            request.header("Content-Type", Client.FormMime);
            request.header("X-Qiniu-Date", headers.map().get("X-Qiniu-Date").toString());
            request.header("Authorization", headers.map().get("Authorization").toString());
            HttpResponse response = request.execute();
            JSONObject resultObj = JSONUtil.parseObj(response.body());
            if(response.getStatus() == 200){
                List<JSONObject> resultObjList = JSONUtil.toList(resultObj.getStr("domains"), JSONObject.class);
                List<JSONObject> tmpList = new ArrayList<>();
                for (JSONObject jsonObject : resultObjList) {
                    if(jsonObject.getStr("platform").equals(platform)){
                        tmpList.add(jsonObject);
                    }
                }
                //返回数据
                return AjaxResult.success("查询七牛云CDN域名列表成功！", tmpList);
            }else{
                return AjaxResult.error("查询七牛云CDN域名列表失败："+resultObj.getStr("error"));
            }
        } catch (Exception e) {
            return AjaxResult.error("查询七牛云CDN域名列表失败："+e.getMessage());
        }
    }


    public static AjaxResult putCdnDomainHttps(String keyId, String keySecret, SslApply apply, String domainTxt, String ptName){
        try {
            Auth auth = createAuth(keyId, keySecret);
            AjaxResult aj = QnySslUtils.uploadSsl(keyId, keySecret, apply, ptName);
            if(aj.isSuccess()){
                //请求参数
                HttpRequest request = HttpRequest.put(QNY_BASE_URI+"/domain/"+domainTxt+"/sslize");
                Map<String,Object> reqParam = new StringMap().map();
                reqParam.put("certid", String.valueOf(aj.get("data")));
                //请求头
                StringMap headers = auth.authorizationV2(QNY_BASE_URI+"/domain/"+domainTxt+"/sslize", "PUT", JSONUtil.toJsonStr(reqParam).getBytes(), Client.JsonMime);
                request.header("Content-Type", Client.JsonMime);
                request.header("X-Qiniu-Date", headers.map().get("X-Qiniu-Date").toString());
                request.header("Authorization", headers.map().get("Authorization").toString());
                //赋值请求参数
                request.body(JSONUtil.toJsonStr(reqParam));
                HttpResponse response = request.execute();
                JSONObject resultObj = JSONUtil.parseObj(response.body());
                if(response.getStatus() == 200){
                    //返回数据
                    return AjaxResult.success("设置七牛云CND域名证书成功！");
                }else{
                    return AjaxResult.error("设置七牛云CND域名证书失败："+resultObj.getStr("error"));
                }
            }
            return aj;
        } catch (Exception e) {
            return AjaxResult.error("设置七牛云CND域名证书失败："+e.getMessage());
        }
    }
}
